package src.mian.java;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class HDFSFileCreateDelete {
    public static void main(String[] args) {
        String hdfsUri = "hdfs://192.168.254.100:8020"; // 替换成您的 HDFS URI
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", hdfsUri);

        try {
            FileSystem fs = FileSystem.get(conf);

            // 创建文件
            Path createFilePath = new Path("/student/age.txt"); // 替换成您要创建的文件路径
            boolean created = fs.createNewFile(createFilePath);
            if (created) {
                System.out.println("File created: " + createFilePath);
            } else {
                System.out.println("Failed to create file: " + createFilePath);
            }
            // 删除文件
            Path deleteFilePath = new Path("/student/age.txt"); // 替换成您要删除的文件路径
            boolean deleted = fs.delete(deleteFilePath, true); // 第二个参数表示是否递归删除文件夹
            if (deleted) {
                System.out.println("File deleted: " + deleteFilePath);
            } else {
                System.out.println("Failed to delete file: " + deleteFilePath);
            }

            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
